package cn.com.cetccst.usercenter.network;

import static cn.com.cetccst.tools.device.Device.getOsv;
import static cn.com.cetccst.usercenter.kv.UserCenterKV.kvStore;

import android.content.Context;
import android.os.Build;
import android.text.TextUtils;

import androidx.annotation.NonNull;

import java.io.IOException;

import cn.com.cetccst.network.IRetrofitClient;
import cn.com.cetccst.network.RetrofitManager;
import cn.com.cetccst.network.log.IHttpLog;
import cn.com.cetccst.usercenter.kv.UserCenterKeys;
import cn.com.cetccst.usercenter.network.response.UserInfoResponse;
import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;
import retrofit2.Retrofit;
import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class UserCenterHttpRequester {

    private static final String MEDIA_JSON_TYPE = "application/json;charset=UTF-8";
    private static final String X_VERSION = "X-version";
    private static final String AUTHENTICATION = "Authentication";
    public static final String PROTO_VERSION = "5.4";
    public static final String API_VERSION_NAME = "version";
    public static final String OS_VERSION = "X-OSVersion";
    public static final String DEVICE_TYPE = "X-DType";
    public static final String BUILD_TYPE = "X-BuildType";

    private static IRetrofitClient retrofitClient;
    private static IHttpLog httpLogConfig;
    private static IUserCenterApiService apiService;
    private static UserCenterHttpRequester httpRequester;

    private UserCenterHttpRequester() {
    }

    public static void initialize(Context context, String baseUrl) {
        if (retrofitClient == null) {
            retrofitClient = new RetrofitManager.Builder(context, baseUrl)
                    .setLogLevel(IHttpLog.Level.BODY)
                    .addInterceptor(new Interceptor() {
                        @NonNull
                        @Override
                        public Response intercept(@NonNull Chain chain) throws IOException {
                            Request originalRequest = chain.request();
                            Request.Builder authorisedRequest = originalRequest.newBuilder();
                            addHeaderIfNecessary(originalRequest, authorisedRequest, X_VERSION, PROTO_VERSION);
                            addHeaderIfNecessary(originalRequest, authorisedRequest, AUTHENTICATION, kvStore != null ? kvStore.decodeString(UserCenterKeys.KEY_ACCESS_TOKEN) : null);
                            addHeaderIfNecessary(originalRequest, authorisedRequest, API_VERSION_NAME, PROTO_VERSION);
                            addHeaderIfNecessary(originalRequest, authorisedRequest, DEVICE_TYPE, Build.BRAND + "_" + Build.MODEL);
                            addHeaderIfNecessary(originalRequest, authorisedRequest, OS_VERSION, getOsv());
                            addHeaderIfNecessary(originalRequest, authorisedRequest, BUILD_TYPE, "du");
                            return chain.proceed(authorisedRequest.build());
                        }
                    })
                    .build();
            Retrofit retrofit = retrofitClient.getRetrofit();
            httpLogConfig = retrofitClient.getHttpLogConfig();
            apiService = retrofit.create(IUserCenterApiService.class);
        }
    }

    private static void addHeaderIfNecessary(Request orgRequest, Request.Builder newRequest, String headerName, String headerContent) {
        if (TextUtils.isEmpty(orgRequest.header(headerName)) && !TextUtils.isEmpty(headerContent)) {
            newRequest.header(headerName, headerContent);
        }
    }

    public static IHttpLog getHttpLogConfig() {
        return httpLogConfig;
    }

    public static UserCenterHttpRequester getInstance() {
        if (httpRequester == null) {
            httpRequester = new UserCenterHttpRequester();
        }
        return httpRequester;
    }

    public Observable<UserInfoResponse> geUserInfo() {
        return apiService.getUserInfo().subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread());
    }

}
